package network;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Vector;

public class MultiChatClientThread extends Thread {
	Socket socket;
	MultiChatClient mcc;
	ObjectOutputStream send;
	ObjectInputStream receive;
	
	public MultiChatClientThread(MultiChatClient mc, Socket s){
		mcc = mc;
		socket = s;
//		this.start();
	}
	
	@Override
	public void run() {
		try {
			send = new ObjectOutputStream(socket.getOutputStream());
			receive = new ObjectInputStream(socket.getInputStream()); 

			SocketData sd = new SocketData("login", mcc.userID.getText(), "님이 로그인합니다", null);
			sendMsg(sd);
			
			while (true) {
				SocketData sdIn = (SocketData) receive.readObject();
//				if (!sdIn.getUser().equals(mcc.userID.getText()))	mcc.dataSet(sd);
				mcc.dataSet(sdIn);
				
				if (sd.getCommand().equals("userlist")) {
					mcc.list.removeAll();
					mcc.clientList = (Vector) sd.getObj();
					mcc.list.setListData(mcc.clientList);
				}
				else if(sdIn.getCommand().equals("serverdown")){
					send.writeObject(sdIn);
					break;
				}
			}
			
			receive.close();
			send.close();

		} catch (Exception e) {
			 e.printStackTrace();
			 return;
		}
	}
	
	public void sendMsg(SocketData sd) {
		try {
			send.writeObject(sd);
			send.flush();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
